<?php
class MessageMapper{
    protected $table;
    protected $query;

    public function setTable($_table){
        $this->table = $_table;
        $this->query = 'SET NAMES utf8; SET CHARACTER SET utf8';
        return $this;
    }
    public function getTable(){
        return $this->table;
    }

    public function save($msg){

        $arr = array(
            'content'   => $msg->getContent(),
            'date'      => $msg->getDate(),
            'u_id'      => $msg->getUserid(),
            'img'       => $msg->getImg(),
            'private'   => $msg->getPrivate(),
            'video'     => $msg->getVideo(),
            'new'       => $msg->getNew()
        );
        
        $db = Database::getInstance();

        $tags = $msg->getTags();
        if(null == ($id = $msg->getId())){
            $db->insert('messages', $arr);
            $mid = $db->selectWhere('messages', 'MAX(id)', '1');
        }else{
            $db->update('messages', $arr, $id);
            $db->deleteWhere('tag_to_msg', 'msg_id = ' . $id);
            
        }
    }
    
    static function countOfNew(){
        $db = Database::getInstance();
        $curuser = UserMapper::getCurrentUser();
        $countarr = $db->selectWhere('messages', 'COUNT(id)', 'private = ' . $curuser->getId() . ' AND new = 1');
        //var_dump($countarr);
        //exit;
        return $countarr[0]['COUNT(id)'];
    }

    public function find($id, $msg){
        $db = Database::getInstance();
        $data = $db->select('messages', $id);
        if($data){
            //$tags = new TagModel();
            $msg    ->setContent($data['content'])
                    ->setDate($data['date'])
                    ->setId($id)
                    ->setVideo($data['video'])
                    ->setMapper($this)
                    ->setImg($data['img'])
                    ->setPrivate($data['private'])
                    ->setNew($data['new'])
                    ->setUserid($data['u_id']);
        }else{
            $err = ErrorHandler::getInstance()->setException('failFindMessage', __LINE__, __FILE__);
            //echo '<script>history.go(-1)</script>';
            header('Location: /WadOS/index/index');
        }
    }

    public function delete($msg){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $db->delete('messages', $msg->getId());
        $db->deleteWhere('tag_to_msg',  'msg_id = ' . $msg->getId());
        $db->deleteWhere('comments',    'msg_id = ' . $msg->getId());
        $db->deleteWhere('imgs',        'msg_id = ' . $msg->getId());
    }

    public function fetchAllMessages($page, $count){
        $db = Database::getInstance();
        
        $resultSet = $db->fetchAll('messages WHERE private = 0 ORDER BY id DESC LIMIT ' . ($page-1)*$count . ', ' . $count);
        
        $messages = array();
        //var_dump($resultSet);
        foreach($resultSet as $one){
            $msg = new MessageModel();
            $msg    ->setContent($one['content'])
                    ->setDate($one['date'])
                    ->setUserid($one['u_id'])
                    ->setId($one['id'])
                    ->setVideo($one['video'])
                    ->setMapper($this);
            $messages[] = $msg;
        }
        return $messages;
        
    }

    public function fetchAllMessagesByTagId($tag_id){
        $db = Database::getInstance();
        $tag = new TagModel();
        $tag = $tag->find($tag_id);
        $resultSet = $db->fetchAll('tag_to_msg WHERE tag_id = "' . $tag->getId() . '"');
        $messages = array();
        
        foreach($resultSet as $one){
            $msg = new MessageModel();
            $msg = $msg->find($one['msg_id']);
            $messages[] = $msg;
        }
        return $messages;
    }

    public function fetchAllMessagesByUserId($u_id){
        $db = Database::getInstance();
        $user = new TagModel();
        $user = $user->find($u_id);
        $resultSet = $db->fetchAll('messages WHERE private = 0 AND u_id = "' . $user->getId() . '"');
        $messages = array();

        foreach($resultSet as $one){
            $msg = new MessageModel();
            //$msg = $msg->find($one['msg_id']);
            $msg    ->setContent($one['content'])
                    ->setDate($one['date'])
                    ->setUserid($one['u_id'])
                    ->setId($one['id'])
                    ->setVideo($one['video'])
                    ->setImg($one['img'])
                    ->setMapper($this);
            $messages[] = $msg;
        }
        return $messages;
    }

    public function fetchAllPrivateMessages($u_id){
        $db = Database::getInstance();
        //$user = new TagModel();
        //$user = $user->find($u_id);
        $resultSet = $db->fetchAll('messages WHERE private = ' . $u_id);
        $messages = array();

        foreach($resultSet as $one){
            $msg = new MessageModel();
            //$msg = $msg->find($one['msg_id']);
            $msg    ->setContent($one['content'])
                    ->setDate($one['date'])
                    ->setUserid($one['u_id'])
                    ->setId($one['id'])
                    ->setPrivate($one['private'])
                    ->setNew($one['new'])
                    ->setVideo($one['video'])
                    ->setImg($one['img'])
                    ->setMapper($this);
            $messages[] = $msg;
        }
        return $messages;
    }

    public function fetchAllSendedMessages($u_id){
        $db = Database::getInstance();
        //$user = new TagModel();
        //$user = $user->find($u_id);
        $resultSet = $db->fetchAll('messages WHERE u_id = ' . $u_id . ' AND private > 0');
        $messages = array();

        foreach($resultSet as $one){
            $msg = new MessageModel();
            //$msg = $msg->find($one['msg_id']);
            $msg    ->setContent($one['content'])
                    ->setDate($one['date'])
                    ->setUserid($one['u_id'])
                    ->setId($one['id'])
                    ->setPrivate($one['private'])
                    ->setNew($one['new'])
                    ->setVideo($one['video'])
                    ->setImg($one['img'])
                    ->setMapper($this);
            $messages[] = $msg;
        }
        return $messages;
    }
}
?>
